package com.key.win.demo.controller;

import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.base.web.Result;
import com.key.win.demo.model.MybatiesTemplate;
import com.key.win.demo.service.MybatiesSqlTemplateService;
import com.key.win.demo.service.MybatiesTemplateService;
import com.key.win.demo.vo.UserOrganVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mybatiesTemplateCtrl/*")
public class MybatiesTemplateCtrl {

    @Autowired
    private MybatiesTemplateService mybatiesTemplateService;

    @Autowired
    private MybatiesSqlTemplateService mybatiesSqlTemplateService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/getMybatiesTemplateByPaged")
    public PageResult<MybatiesTemplate> getMybatiesTemplateByPaged(@RequestBody PageRequest<MybatiesTemplate> t) {
        return mybatiesTemplateService.findMybatiesTemplateByPaged(t);
    }

    @PostMapping("/getUserOrganByPaged")
    public PageResult<UserOrganVo> getUserOrganByPaged(@RequestBody PageRequest<UserOrganVo> t) {
        return mybatiesTemplateService.getUserOrganByPaged(t);
    }


    @GetMapping("/addRedis/{msg}")
    public Result addRedis(@PathVariable String msg) {
        redisTemplate.opsForValue().set("test", msg);
        return Result.succeed(redisTemplate.opsForValue().get("test"), "");
    }

    @GetMapping("/getCacheTest")
    public Result getCacheTest() {
        return Result.succeed(mybatiesTemplateService.getCacheTest());
    }


    @GetMapping("/getAll")
    public Result getAll() {
        return Result.succeed(mybatiesSqlTemplateService.getAll(), "");
    }


    @PostMapping("/saveOrUpdateMybatiesTemplate")
    public Result saveOrUpdateMybatiesTemplate(@RequestBody MybatiesTemplate mybatiesTemplate) {
        boolean b = mybatiesTemplateService.saveOrUpdate(mybatiesTemplate);
        return Result.result(b);
    }

    @GetMapping("/get/{id}")
    public MybatiesTemplate get(@PathVariable String id) {
        return mybatiesTemplateService.getById(id);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean b = mybatiesTemplateService.removeById(id);
        return Result.result(b);
    }


}
